package com.itcam.cammydada.test;

import io.reactivex.Flowable;
import io.reactivex.Scheduler;
import io.reactivex.Single;
import io.reactivex.schedulers.Schedulers;

import java.sql.Time;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class RxJavaDemo {

    public static void main(String[] args) throws InterruptedException {
//        extracted();
        CountDownLatch latch = new CountDownLatch(1);

        Flowable<Long> flowable = Flowable.interval(1, TimeUnit.SECONDS)
                .map(i -> i + 1)
                .take(12)
                .subscribeOn(Schedulers.io());

        flowable.observeOn(Schedulers.io())
                .doOnNext(item -> System.out.println("item = " + item))
                .doOnComplete(() -> {
                    latch.countDown();
                })
                .subscribe();
        latch.await();
        Thread.sleep(10000L);
    }


    private static void extracted() {
        //        Single<List<Integer>> list = Flowable.range(0, Integer.MAX_VALUE)
//                .toList();
//        // 订阅并消费数据
//        list.subscribe(
//                integers -> {
//                    System.out.println("列表大小: " + integers.size());
//                    // 遍历列表中的每个元素
//                    for (Integer num : integers) {
//                        System.out.println("元素: " + num);
//                    }
//                },
//                error -> System.err.println("发生错误: " + error.getMessage())
//        );

        Flowable<Integer> flowable1 = Flowable.just(10, 2, 4);
        Flowable<Integer> flowable2 = Flowable.just(9, 1, 8);
        Flowable<Integer> mergedFlowable = Flowable.concat(flowable1, flowable2);
        Flowable<Integer> sorted = mergedFlowable.sorted();
        for (Integer i : sorted.blockingIterable()) {
            System.out.print(i + " ");
        }
    }
}
